package com.fzy.modules.bill.service;

import com.fzy.core.common.IDUtils;
import com.fzy.core.common.PageObject;
import com.fzy.modules.bill.dao.SoBillDao;
import com.fzy.modules.bill.dao.WsBillDao;
import com.fzy.modules.bill.entity.SoBill;
import com.fzy.modules.people.dao.CustomerDao;
import com.fzy.modules.people.entity.Customer;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
 * 销售订单操作
 */
@Service
public class SoBillService {
    @Autowired
    SoBillDao soBillDao;
    @Autowired
    CustomerDao customerDao;
    @Autowired
    WsBillDao wsBillDao;

    /**
     * 分页查询所有销售订单
     */
    public PageObject<SoBill> findAllSoBill(SoBill soBill, int page, int rows){
        PageHelper.startPage(page, rows);
        List<SoBill> sobills = soBillDao.findAllSoBills(soBill);
        //查询之后获取分页对象
        PageInfo<SoBill> pageInfo=new PageInfo<SoBill>(sobills);
        //构建我们自定义的分页实体返回类
        PageObject<SoBill> pageObject=new PageObject<SoBill>();
        pageObject.setTotal(pageInfo.getTotal());
        pageObject.setRows(sobills);
        return pageObject;
    }
    public List<SoBill> findAllSoBill(){
        SoBill soBill=new SoBill();
        soBill.setBillStatus(0);
        return soBillDao.findAllSoBills(soBill);
    }
    /**
     * 审核或未审核
     */
    public Boolean checkOut(Integer status,String id){
        SoBill soBill=new SoBill();
        soBill.setId(id);
        soBill.setBillStatus(status);
        soBill.setConfirmDate(new Date());
      /*  WsBill ws=new WsBill();
        ws.setId(IDUtils.genItemId());
        ws.setBillStatus(0);//如果是店面直接出库则为1000
        ws.setBizdt(new Date());
        ws.setBizUserId(so.getBizUserId());//业务员暂时写销售订单的业务员
        ws.setCustomerId(so.getCustomerId());
        ws.setDateCreated(new Date());
        ws.setInputUserId(so.getInputUserId());//制单人暂时写销售订单的制单人
        List<WsBill> wsBills=wsBillDao.findWsBillList(new WsBill());
        ws.setRef(getWsBillRef("WS",wsBills));
        ws.setSaleMoney(so.getMoneyWithTax());//销售金额还没确定写纯销售金额还是价税合计
        ws.setWarehouseId("");//仓库地址根据页面选择
        ws.setReceivingType(so.getReceivingType());
        ws.setDataOrg(so.getDataOrg());
        ws.setCustomerId(so.getCompanyId());
        if(StringUtils.isNotEmpty(so.getBillMemo())){
            ws.setMemo(so.getBillMemo());
        }
        ws.setDealAddress(so.getDealAddress());*/
        return soBillDao.checkOut(soBill);
    }
    /**
     * 查询某个客户是否有销售订单
     */
    public Integer isWithCustomer(String id){
        return soBillDao.isWithCustomer(id);
    }
    /**
     * 添加销售订单
     */
    public Integer save(SoBill soBill){
        soBill.setId(IDUtils.genItemId());
        if(StringUtils.isEmpty(soBill.getCompanyId())){
            soBill.setCompanyId("4D74E1E4-A129-11E4-9B6A-782BCBD7746B");
        }
        if(StringUtils.isEmpty(soBill.getDataOrg())){
            soBill.setDataOrg("01010001");
        }
        Customer c=new Customer();
        c.setId(soBill.getCustomerId());
        List<Customer> list=customerDao.findAll(c);
        c=list.get(0);
        soBill.setContact(c.getContact01());
        soBill.setTel(c.getTel01());
        if(StringUtils.isNotEmpty(c.getFax())){
            soBill.setFax(c.getFax());
        }
        soBill.setRef(getRef());
        soBill.setBillStatus(0);
        soBill.setDateCreated(new Date());
        soBill.setBizDt(new Date());
        return soBillDao.insert(soBill);

    }
    /**
     * 生成ref
     */
    public String getRef(){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMDD");
        String st=sdf.format(new Date()).toString();
        String head="SO";
        String refI="";
        List<SoBill> soBills=soBillDao.findAllSoBills(new SoBill());
        if(soBills != null && soBills.size()>0){
            int size=0;
            for(int i=0;i<soBills.size();i++) {
                refI = soBills.get(i).getRef().substring(2, 10);
                if(refI.equals(st)){
                    int refSize = Integer.parseInt(soBills.get(i).getRef().substring(10));
                    if(size<refSize){
                        size=refSize;
                    }
                }
            }
                if(size>0 ){
                    if( size<9){
                        return head + st + "00" +(size+1);
                    }else if(size>=9 && size<99){
                        return head + st + "0" +(size+1);
                    }else{
                        return head + st + (size+1);
                    }
                }else {
                    return head + st + "001";
                }
            }
            return head + st + "001";
        }
	public List<SoBill> findSoBillByInputUserId(String inputUserId, Integer billStatus, int page, int rows) {
		 PageHelper.startPage(page, rows);
		 if(inputUserId==null||billStatus==null){
			 return null;
		 }
		 return soBillDao.findSoBillByInputUserId(inputUserId,billStatus);
	}
}
